import AJAX from '@/utils/ajax'
import Playlog from '@/components/playlog'
import Love from '@/components/love'
import { isLogin } from '@/utils'

class User {
  constructor() {}

  time() {
    let countdown = 60
    const that = this
    if (countdown == 0) {
      val.prop('disabled', false)
      val.val('获取验证码')
      countdown = 60
      return true
    } else {
      val.prop('disabled', true)
      val.val('重新发送(' + countdown + ')')
      countdown--
    }
    setTimeout(() => {
      that.time(val)
    }, 1000)
  }

  userinfo() {
    if (isLogin()) {
      AJAX({ url: 'flushinfo' }).then((res) => {
        console.log(res)
        if (res.code > 0) {
          $('#playlog-todo').html('<a target="_blank" href="' + cms.root + 'index.php?s=/user/center/playlog">进入会员中心查看播放记录&gt;&gt;</a>')
          $('#user_login').html(res.html).show()
          $('#loginbarx').hide()
          $('.logoutbt').unbind()
          this.quit()
        }
      })
    }
  }

  refresh(obj) {
    obj.attr('src', AJAX({ url: 'verify', data: { s: Math.random() } }))
  }

  verify() {
    this.refresh($('img.validate-img'))
  }

  // 发送验证码
  send() {
    const that = this
    $('body').on('click', '#sendsms', function (e) {
      e.preventDefault()
      const to = $('input[name="to"]').val()
      const ac = $('input[name="ac"]').val()
      const type = $('input[name="type"]').val()
      if ('' == to) {
        layer.msg('请输入手机号码')
        $('#to').focus()
        return false
      }
      const pattern = /^[1][0-9]{10}$/
      const ex = pattern.test(to)
      if (!ex) {
        layer.msg('手机号格式不正确')
        $('#to').focus()
        return false
      }

      AJAX({ url: 'send', method: 'post', data: { to, ac, type } }).then((res) => {
        if (res.code > 0) {
          that.time($(this))
        } else {
          layer.msg(res.msg)
        }
      })
    })
  }

  login() {
    const that = this
    $('body').on('click', '#login-submit', function () {
      const user_name = $('#user_name').val()
      const user_password = $('#user_password').val()
      const validate = $('#validate').val()
      const params = $('#user-login').serializeArray()
      console.log(params)
      if (user_name == '') {
        layer.msg('请输入用户名手机或邮箱')
        $('#username').focus()
        return false
      } else if (user_password == '') {
        layer.msg('请输入密码')
        $('#password').focus()
        return false
      } else if (validate == '') {
        layer.msg('请输入验证码')
        $('#validate').focus()
        return false
      } else {
        AJAX({ url: 'login', method: 'post', data: params }).then((res) => {
          if (res.code > 0) {
            new Playlog()
            new Love()
            that.verify()
            layer.closeAll()
          } else {
            layer.msg(res.msg)
            that.verify()
          }
        })
      }
    })
  }

  reg() {
    const that = this
    $('body').on('click', '#reg-submit', function () {
      const ac = $('input[name="ac"]').val()
      const to = $('input[name="to"]').val()
      const code = $('input[name="code"]').val()
      const password = $('input[name="user_password"]').val()
      const validate = $('#user-reg input[name="validate"]').val()
      const params = $('#user-reg').serializeArray()
      delete params.reuser_password
      if (ac == 'mobile') {
        if (to == '') {
          layer.msg('请输入手机号码')
          $('#user_name').focus()
          return false
        }
        const pattern = /^[1][0-9]{10}$/
        const ex = pattern.test(to)
        if (!ex) {
          layer.msg('手机号格式不正确')
          $('#user_name').focus()
          return false
        }
      }
      if ($('#user_name').val() == '') {
        layer.msg('请输入用户名')
        $('#user_name').focus()
        return false
      } else if (password == '') {
        layer.msg('请输入密码')
        $('#user_password').focus()
        return false
      } else if (!(password == $('#reuser_password').val())) {
        layer.msg('两次输入的密码不一至')
        $('#reuser_password').focus()
        return false
      } else if (code == '') {
        layer.msg('手机验证码不能为空')
        $('#code').focus()
        return false
      } else if (validate == '') {
        layer.msg('图形验证码不能为空')
        $('#validate').focus()
        return false
      }
      AJAX({ url: 'reg', method: 'post', data: params }).then((res) => {
        if (res.code > 0) {
          new Playlog()
          new Love()
          that.verify()
          layer.closeAll()
        } else {
          layer.msg(res.msg)
          that.verify()
        }
      })
    })
  }

  // 弹出登录窗口
  loginform() {
    if (!isLogin()) {
      layer.closeAll()
      AJAX({ url: 'loginhtml', data: { type: 'pc' } }).then((res) => {
        layer.open({
          type: 1,
          title: false,
          closeBtn: 1, //不显示关闭按钮
          anim: 2,
          shadeClose: false, //开启遮罩关闭
          content: res,
        })
        this.login()
        this.addsns()
      })
    } else {
      return false
    }
  }

  rgeform() {
    if (!isLogin()) {
      layer.closeAll()
      AJAX({ url: 'reghtml', data: { type: 'pc' } }).then((res) => {
        layer.open({
          type: 1,
          title: false,
          closeBtn: 1, //不显示关闭按钮
          anim: 2,
          shadeClose: false, //开启遮罩关闭
          content: res,
        })
        this.reg()
        this.addsns()
        this.send()
      })
    } else {
      return false
    }
  }

  addsns() {
    $('body').on('click', '#addsns', function () {
      const type = $(this).attr('data-id')
      const snsckeck = setInterval(snslogin, 1000)
      window.open(AJAX({ url: 'snslogin', data: { type } }), '_blank', 'width=750, height=525')
      function snslogin() {
        if (isLogin()) {
          new Playlog()
          new Love()
          layer.closeAll()
          clearInterval(snsckeck)
        } else {
          return false
        }
      }
    })
  }

  quit() {
    $('body').on('click', '.logoutbt', function () {
      AJAX({ url: 'logout' }).then((res) => {
        window.location.reload()
      })
      return false
    })
  }
}

export default User
